//package com.apcc4m.sfpushbatch;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.UUID;
//
//import org.springframework.batch.core.Job;
//import org.springframework.batch.core.Step;
//import org.springframework.batch.core.StepContribution;
//import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
//import org.springframework.batch.core.scope.context.ChunkContext;
//import org.springframework.batch.core.step.tasklet.Tasklet;
//import org.springframework.batch.repeat.RepeatStatus;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.annotation.Bean;
//import org.springframework.data.mongodb.core.MongoTemplate;
//
//import com.apcc4m.sfpushbatch.repository.Notification;
//import com.apcc4m.sfpushbatch.repository.NotificationRepository;
//import com.apcc4m.sfpushbatch.repository.PushStatus;
//import com.apcc4m.sfpushbatch.repository.PushStatusRepository;
//import com.apcc4m.sfpushbatch.repository.User;
//import com.apcc4m.sfpushbatch.repository.UserRepository;
//
//@SpringBootApplication
//@EnableBatchProcessing
//public class SampleMongoApplication {
//
//    @Autowired
//    private MongoTemplate mongoTemplate;
//    
//    @Autowired
//    private NotificationRepository notificationRepository;
//    @Autowired
//    private UserRepository userRepository;
//    @Autowired
//    private PushStatusRepository pushStatusRepository;
//    
//    @Autowired
//    private JobBuilderFactory jobs;
//
//    @Autowired
//    private StepBuilderFactory steps;
//
//    @Bean
//    protected Tasklet tasklet() {
//        return new Tasklet() {
//            @Override
//            public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
//                System.out.println("Tasklet execute");
//                mongoTemplate.dropCollection(User.class);
//                long t1 = System.currentTimeMillis();
//
//                List<User> userList = new ArrayList<User>();
//                int type = 1;
//                for(int i=0;i<1000000;i++){
//                    User user = new User();
//                    user.setDeviceId(UUID.randomUUID().toString());
//                    user.setStatus(1);
//                    user.setType(type);
//                    type = type == 1 ? 2 : 1;
//                    userList.add(user);
//                }
//                mongoTemplate.insertAll(userList);
//                long t2 = System.currentTimeMillis();
//                System.out.println(t2-t1);
////                repository.insert(userList.iterator());
////                repository.save(userList.iterator());
////                Notification n1 = new Notification();
////                n1.setTitle("title");
////                n1.setType(1);
////                n1.setStatus(0);
////                n1.setContent("content");
////                n1.setPushDate(new Date());
////                repository.save(n1);
//                
//                return RepeatStatus.FINISHED;
//            }
//        };
//    }
//
//    @Bean
//    protected Tasklet tasklet2() {
//        return new Tasklet() {
//            @Override
//            public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
//                System.out.println("Tasklet execute2");
//                long t1 = System.currentTimeMillis();
//                List<User> userList = userRepository.findAll();
//                List<PushStatus> pushStatusList = new ArrayList<PushStatus>();
//                long t2 = System.currentTimeMillis();
//                System.out.println("查询数据花费时间："+(t2-t1));
//                int status = 1;
//                for(User item: userList) {
//                    PushStatus pushStatus = new PushStatus();
//                    pushStatus.setUserId(item.getId());
//                    pushStatus.setStatus(status);
//                    pushStatus.setnId("56ebb4e4753251fe19a31305");
//                    pushStatusList.add(pushStatus);
//                }
//
//                mongoTemplate.insertAll(pushStatusList);
//                long t3 = System.currentTimeMillis();
//                System.out.println("保存数据花费时间："+(t3-t2));
//                return RepeatStatus.FINISHED;
//            }
//        };
//    }
//
//    @Bean
//    protected Tasklet tasklet3() {
//        return new Tasklet() {
//            @Override
//            public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
//                mongoTemplate.dropCollection(Notification.class);
//                mongoTemplate.dropCollection(PushStatus.class);
//                createNotifcation("title01","content01");
//                createNotifcation("title02","content02");
//                createNotifcation("title03","content03");
////                Notification notificationList = notificationRepository.findOneByStatusOrderByPushDateAsc(0);
////                for(Notification item: notificationList){
////                    System.out.println(notificationList.getTitle());
////                }
//                return RepeatStatus.FINISHED;
//            }
//        };
//    }
//    
//    public void createNotifcation(String title,String content){
//        Notification n1 = new Notification();
//        n1.setTitle(title);
//        n1.setType(1);
//        n1.setStatus(0);
//        n1.setContent(content);
//        n1.setInsertDate(new Date());
//        n1.setPushDate(new Date());
//        notificationRepository.save(n1);
//    }
//    
//    @Bean
//    public Job job1() throws Exception {
//        System.out.println("Job job1");
//        return jobs.get("job1").start(step1()).build();
//    }
//
//    @Bean
//    protected Step step1() throws Exception {
//        System.out.println("Step step1");
//        return this.steps.get("step1").tasklet(tasklet3()).build();
//    }
//
//    public static void main(String[] args) throws Exception {
//        System.exit(SpringApplication.exit(SpringApplication.run(SampleMongoApplication.class, args)));
//    }
//
//}
